const service = require('../../utils/service.js');
const api = require('../../utils/util.js');
Page({
  data: {
    shopData: {},
    userInfo: {},
    shopName: '',
    shopLogo: '',
    address: '',
    description: '',
    bannerList: [],
    link_arr: [],
    idx: '',  //获取点击添加商品链接图片的index
    selectType: false,

  },
  onLoad: function(options) {
    let userInfo = wx.getStorageSync('userInfo')
    this.setData({
      userInfo: userInfo
    })
    if (JSON.stringify(options) != '{}') {
      // console.log(options)
    }
    this.getShopInfo(() => {
      this.getBanner()
    })
    wx.removeStorageSync('goodsLink')
  },

  onShow: function() {
    let link_arr = wx.getStorageSync('goodsLink')
    this.setData({
      link_arr: link_arr
    })
    let fuck = this.data.bannerList
    if (fuck) {
      if (link_arr.length != 0) {
        for (var i = 0, len = link_arr.length; i < len; i++) {
          fuck[link_arr[i].index].name = link_arr[i].name
          fuck[link_arr[i].index].link_type_id = link_arr[i].goods_id
          fuck[link_arr[i].index].link_type = link_arr[i].link_type
        }
      }
      this.setData({
        bannerList: fuck
      })
    }

  },

  // 获取店铺信息
  getShopInfo: function(cb) {
    let that = this;
    let shopData = wx.getStorageSync('shop_data');
    api.ajax(service.url('getShopDetail'), {
      userid: wx.getStorageSync('out_user_id')
    }, function(res) {
      that.setData({
        shopData: shopData,
        shopInfo: res.data,
        address: res.data.shop_location,
        description: res.data.shop_intro,
        shopName: res.data.shop_name,
        shopLogo: res.data.shop_logo
      })
      cb()
    })
  },

  // 获取banner列表
  getBanner: function() {
    let that = this
    api.ajax(service.url('shopbanner'), {
      unionid: this.data.shopInfo.unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      shop_code: this.data.shopInfo.shop_code,
      position: 0
    }, function(res) {
      if (res.code == 0) {
        that.setData({
          bannerList: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },

  // 获取店铺名字
  getShopName: function(e) {
    let shop_name = e.detail.value
    this.setData({
      shopName: shop_name
    })
  },

  // 获取店铺简介
  getDesc: function(e) {
    let val = e.detail.value
    this.setData({
      description: val
    })
  },

  // 选择地址
  bindRegionChange: function(e) {
    let address_arr = e.detail.value
    let address = ''
    address_arr.forEach(function(item, index) {
      address += item + ' '
    })
    this.setData({
      address: address
    })
  },

  // 编辑店铺
  editShop: function() {
    let that = this
    let shop_name = this.data.shopName
    if (shop_name == '' || shop_name == null) {
      api.toast('请填写店铺名字')
      return
    }

    this.saveBanner()
    api.ajax(service.url('shopEdit'), {
      unionid: this.data.shopInfo.unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      shop_code: this.data.shopInfo.shop_code,
      userid: wx.getStorageSync('out_user_id'),
      shop_name: shop_name,
      shop_logo: this.data.shopLogo,
      shop_intro: this.data.description,
      shop_location: this.data.address
    }, function(res) {
      if (res.code == 0) {
        api.toast('编辑店铺成功')
        setTimeout(() => {
          that.onLoad()
          getCurrentPages()[getCurrentPages().length - 2].onLoad()
          wx.navigateBack({
            delta: -1
          })
        }, 1000)
      } else {
        api.toast(res.message)
      }
    })
  },

  // 保存店铺banner
  saveBanner: function() {
    let that = this
    let list = this.data.bannerList
    let str = ''
    for (var i = 0, len = list.length; i < len; i++) {
      str += `name^${list[i].name}^url^${list[i].url}^position^0^link_type^${list[i].link_type}^link_type_id^${list[i].link_type_id}|`
      console.log(str);
    }
    wx.showLoading()
    api.ajax(service.url('addBatch'), {
      unionid: this.data.shopInfo.unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      shop_code: this.data.shopInfo.shop_code,
      batch_str: str
    }, function(res) {
      if (res.code == 0) {} else {
        api.toast(res.message)
      }
    })
  },

  // 选择店铺logo
  selectLogo: function() {
    let that = this
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      success: (res) => {
        if (res.tapIndex == 0) {
          api.upLoadImg(1, 'album', function(res) {
            that.setData({
              shopLogo: res[0]
            })
          })
        } else {
          api.upLoadImg(1, 'camera', function(res) {
            that.setData({
              shopLogo: res[0]
            })
          })
        }
      }
    })
  },

  // 选择首页展示图片
  selectBanner: function() {
    let that = this
    let leng = this.data.bannerList.length
    if (leng == 5) {
      return
    }
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      success: (res) => {
        if (res.tapIndex == 0) {
          api.upLoadImg(5 - leng, 'album', function(res) {
            for (var i = 0, len = res.length; i < len; i++) {
              let obj = {
                url: res[i]
              }
              that.data.bannerList.push(obj)
            }
            that.setData({
              bannerList: that.data.bannerList
            })
          })
        } else {
          api.upLoadImg(5 - leng, 'camera', function(res) {
            that.setData({
              shopLogo: res[0]
            })
          })
        }
      }
    })
  },

  // 移除图片
  delImg: function(e) {
    let idx = e.currentTarget.dataset.idx
    this.data.bannerList.splice(idx, 1)

    this.setData({
      bannerList: this.data.bannerList
    })
  },

  // 添加商品链接
  linkGoods: function(e) {
    let idx = e.currentTarget.dataset.idx;
    this.setData({
      idx: idx,
      selectType: true
    })
  },

  colsePop: function() {
    this.setData({
      selectType: false
    })
  },

  //商品banner图片链接关联商品详情或分类列表
  toLink: function(e) {
    let linkId = e.currentTarget.dataset.linkId;
    console.log(linkId);
    let idx = this.data.idx;

    this.setData({
      selectType: false
    })
    if (linkId == 1) {
      //选择关联商品
      wx.navigateTo({
        url: `../select_goods/select_goods?index=${idx}`,
      })
    } else {
      //选择关联分类
      wx.navigateTo({
        url: `./classify_list/classify_list?index=${idx}`,
      })
    }
  },
})